package regpang.myapplication22.animotion;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import regpang.myapplication22.R;

public class AnimationToViewActivity extends AppCompatActivity {

    private LinearLayout frameLayout;
    private ImageView imageView;
    private TextView textView;
    private Button button;
    private int hideHeight = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation_to_view);
        frameLayout = findViewById(R.id.fl_content);
        textView = findViewById(R.id.tv_text);
        imageView = findViewById(R.id.iv_image);
        button = findViewById(R.id.button);
        imageView.setVisibility(View.GONE);
//        final HiddenAnimUtils hiddenAnimUtils = HiddenAnimUtils.newInstance(AnimationToViewActivity.this, imageView.getHeight(), imageView, button);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (hideHeight == 0) {
//                    imageView.setVisibility(View.VISIBLE);
//                    frameLayout.setAnimation(AnimationUtils.loadAnimation(AnimationToViewActivity.this, R.anim.an_buttom_to_up));
//                    hideHeight = imageView.getHeight();
//                } else {
                if (hideHeight == 0) {
                    hideHeight = imageView.getHeight();
                }
                startAnim(imageView.getVisibility() == View.GONE, frameLayout, imageView, hideHeight);
//                }
//                hiddenAnimUtils.toggle();
            }
        });


    }

    /**
     * 设置展开的动画
     *
     * @param isOpen 是否要展开
     * @param setAnimView 设置动画的View
     * @param view 隐藏显示操作的View
     * @param hintHeight 隐藏的高度，在需要隐藏的布局还没有隐藏时获取他的高度
     */
    private void startAnim(final boolean isOpen, final View setAnimView, final View view, int hintHeight) {
        TranslateAnimation translateAnimation = null;
        if (isOpen) {
            view.setVisibility(View.VISIBLE);
            if (hintHeight == 0){
                setAnimView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.an_buttom_to_up));
                return;
            }else {
                translateAnimation = new TranslateAnimation(0, 0, hintHeight, 0);
            }
        } else {
            translateAnimation = new TranslateAnimation(0, 0, 0, hintHeight);
        }
        translateAnimation.setDuration(2000);
        setAnimView.startAnimation(translateAnimation);
//                    Animation animation = AnimationUtils.loadAnimation(AnimationToViewActivity.this, R.anim.an_up_to_buttom);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (view.getVisibility() == View.VISIBLE && !isOpen) {
                    view.setVisibility(View.GONE);
                }
                setAnimView.clearAnimation();

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
